
const path = require('path');
const fs = require('fs')

module.exports = {
    array: {
        delete: {
            byIndex(arr = [], index = -1) {
                if (check.isArray(arr) == false) {
                    check.throwError('arr应为数组')
                }
                if (check.isInt(index) == false) {
                    check.throwError('index应为整数')
                }
                return [...arr.slice(0, index), ...arr.slice(index + 1)]
            },
            byKeyValue(arr = [], key = '', value = undefined) {
                if (check.isArray(arr) == false) {
                    check.throwError('arr应为数组')
                }
                return arr.filter(item => item[key] != value)
            },
            byValue(arr = [], value = undefined) {
                if (check.isArray(arr) == false) {
                    check.throwError('arr应为数组')
                }
                return arr.filter(item => item != value)
            },
            clearUndefined(arr = []) {
                if (check.isArray(arr) == false) {
                    check.throwError('arr应为数组')
                }
                return arr.filter(item => item != undefined)
            }
        },
        async format(arr = [], func = async (e) => { return e }) {
            if (check.isArray(arr) == false) {
                check.throwError('arr应为数组')
            }
            for (let i = 0; i < arr.length; i++) {
                arr[i] = await func(arr[i])
            }
            return arr
        },
        count(arr, value) {
            let re = 0
            for (let i = 0; i < arr.length; i++) {
                if (arr[i] === value) re++
            }
            return re
        },
        getFirstIndex: {
            byKeyValue(arr = [], key = '', value = undefined) {
                for (let i = 0; i < arr.length; i++) {
                    if (arr[i][key] === value) return i
                }
                return -1
            }
        }
    },
    object: {
        async for(obj = {}, func = async (key, value) => { return value }) {
            for (let i = 0; i < Object.keys(obj).length; i++) {
                obj[Object.keys(obj)[i]] = await func(Object.keys(obj)[i], obj[Object.keys(obj)[i]])
            }
            return obj
        }
    },
    string: {
        delete: {
            bySubString(string, subString) {
                string = string.split(subString)
                let re = ''
                for (let i = 0; i < string.length; i++) {
                    re += string[i]
                }
                return re
            }
        }
    },
    JSON: {
        parse(string) {
            try {
                return JSON.parse(string)
            } catch (error) {
                return string
            }
        },
        stringify(obj) {
            return JSON.stringify(obj)
        }
    },
    sliceByPageSize(arr, page, size) {
        return arr.slice((page - 1) * size, page * size)
    },
    encodeString(string) {

        function isLetter(code) {
            if (code >= 65 && code <= 90) {
                return 1
            }
            if (code >= 97 && code <= 122) {
                return 1
            }
            if (code > 90 && code < 97) {
                return 2
            }
            if (code < 65) {
                return 0
            }
            return 2
        }

        function getNumberString(n, x) {
            n = JSON.stringify(n)
            for (let i = n.length; i < x; i++) {
                n = '0' + n
            }
            return n
        }

        string = string.split('')
        let string1 = ''
        for (let i = 0; i < string.length; i++) {
            string1 += JSON.stringify(JSON.stringify(string[i].charCodeAt(0)).length) + JSON.stringify(string[i].charCodeAt(0))
        }
        string1 = string1.split('')
        for (let i = 0; i < string1.length; i++) {
            if (string1[i] == '0') {
                continue
            }
            let c = i
            let ss = string1[c]
            while (true) {
                let cd = isLetter(parseInt(ss))
                if (cd == 2) {
                    break
                }
                if (cd == 1) {
                    string1[c] = String.fromCharCode(parseInt(ss))
                    for (let i1 = i; i1 < c; i1++) {
                        string1[i1] = '-'
                    }
                    i = c
                    break
                }
                c++
                if (c == string1.length) {
                    break
                }
                ss += string1[c]
            }
        }
        for (let i = 0; i < string1.length - 1; i++) {
            if (string1[i] == '2' && string1[i + 1] == '2') {
                string1[i] = '?'
                string1[i + 1] = '-'
            }
        }
        let string2 = []
        for (let i = 0; i < string1.length; i++) {
            if (string1[i] == '-') {
                continue
            }
            string2.push(JSON.stringify((string1[i].charCodeAt(0) - string1[i].charCodeAt(0) % 52) / 52) + getNumberString(string1[i].charCodeAt(0) % 52, 2))
        }
        let string3 = ''
        for (let i = string2.length - 1; i >= 0; i--) {
            string3 += string2[i]
        }
        string3 = string3.split('')
        let string4 = []
        for (let i = 0; i < string3.length; i += 2) {
            let ch = string3[i]
            if (i + 1 != string3.length) {
                ch += string3[i + 1]
            }
            if (parseInt(ch) < 52 && ch.length == 2) {
                if (parseInt(ch) < 26) {
                    string4.push(String.fromCharCode(parseInt(ch) + 65))
                } else {
                    string4.push(String.fromCharCode(parseInt(ch) + 97 - 26))
                }
            } else {
                string4.push(ch)
            }
        }
        let string5 = ''
        for (let i = 0; i < string4.length; i++) {
            string5 += string4[i]
        }
        return string5
    },
    decodeString(string) {
        function getNumberString(n, x) {
            n = JSON.stringify(n)
            for (let i = n.length; i < x; i++) {
                n = '0' + n
            }
            return n
        }

        string = string.split('')
        for (let i = 0; i < string.length; i++) {
            if (string[i].charCodeAt(0) >= 48 && string[i].charCodeAt(0) <= 56) {
                continue
            }
            if (string[i].charCodeAt(0) <= 90) {
                string[i] = getNumberString(string[i].charCodeAt(0) - 65, 2)
            } else {
                string[i] = getNumberString(string[i].charCodeAt(0) - 97 + 26, 2)
            }
        }
        let string2 = ''
        for (let i = 0; i < string.length; i++) {
            if (string[i] == '-8') {
                string2 += '9'
                continue
            }
            string2 += string[i]
        }
        string2 = string2.split('')
        let string3 = []
        for (let i = 0; i < string2.length; i += 3) {
            string3.push(getNumberString(parseInt(string2[i]) * 52 + parseInt(string2[i + 1] + string2[i + 2]), 3))
        }
        let string4 = []
        for (let i = string3.length - 1; i >= 0; i--) {
            string4.push(String.fromCharCode(parseInt(string3[i])))
        }
        let string5 = ''
        for (let i = 0; i < string4.length; i++) {
            if (string4[i] == '?') {
                string5 += '22'
                continue
            }
            if ((string4[i].charCodeAt(0) >= 48 && string4[i].charCodeAt(0) <= 57) == false) {
                string5 += JSON.stringify(string4[i].charCodeAt(0))
                continue
            }
            string5 += string4[i]
        }
        string5 = string5.split('')
        let string6 = ''
        for (let i = 0; i < string5.length; i++) {
            let n = parseInt(string5[i])
            let s = ''
            for (let i1 = 0; i1 < n; i1++) {
                s += string5[i + 1 + i1]
            }
            string6 += String.fromCharCode(parseInt(s))
            i += n
        }
        return string6
    },
    formatDate2(date) {
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，+1调整
        const day = date.getDate().toString().padStart(2, '0');
        const hours = date.getHours().toString().padStart(2, '0');
        const minutes = date.getMinutes().toString().padStart(2, '0');
        const seconds = date.getSeconds().toString().padStart(2, '0');

        return `${year}${month}${day}${hours}${minutes}${seconds}`;
    },
    formatDateTime(date) {
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，+1调整
        const day = date.getDate().toString().padStart(2, '0');
        const hours = date.getHours().toString().padStart(2, '0');
        const minutes = date.getMinutes().toString().padStart(2, '0');
        const seconds = date.getSeconds().toString().padStart(2, '0');

        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    formatTimeNew(inputTime = new Date()) {
        function getPeriod(hours) {
            if (hours >= 0 && hours < 6) {
                return '凌晨';
            } else if (hours >= 6 && hours < 11) {
                return '上午';
            } else if (hours >= 11 && hours < 14) {
                return '中午';
            } else if (hours >= 14 && hours < 18) {
                return '下午';
            } else {
                return '晚上';
            }
        }

        function getWeekName(n) {
            switch (n) {
                case 0:
                    return '星期天'
                case 1:
                    return '星期一'
                case 2:
                    return '星期二'
                case 3:
                    return '星期三'
                case 4:
                    return '星期四'
                case 5:
                    return '星期五'
                case 6:
                    return '星期六'
            }
        }
        let currentTime = new Date()
        let oneDaySecond = 86400000
        let currentDayStart = new Date(currentTime.getTime() - currentTime.getHours() * 60 * 60 * 1000 - currentTime
            .getMinutes() * 60 * 1000 - currentTime.getSeconds() * 1000)
        let lastDayStart = new Date(currentTime.getTime() - currentTime.getHours() * 60 * 60 * 1000 - currentTime
            .getMinutes() * 60 * 1000 - currentTime.getSeconds() * 1000 - oneDaySecond)
        let nextDayStart = new Date(currentTime.getTime() - currentTime.getHours() * 60 * 60 * 1000 - currentTime
            .getMinutes() * 60 * 1000 - currentTime.getSeconds() * 1000 + oneDaySecond)
        let nextDayEnd = new Date(currentTime.getTime() - currentTime.getHours() * 60 * 60 * 1000 - currentTime
            .getMinutes() * 60 * 1000 - currentTime.getSeconds() * 1000 + oneDaySecond * 2)
        let prefix = ''
        if (inputTime >= currentDayStart && inputTime < nextDayStart) {
            prefix = '今天'
        }
        if (inputTime >= lastDayStart && inputTime < currentDayStart) {
            prefix = '昨天'
        }
        if (inputTime >= nextDayStart && inputTime < nextDayEnd) {
            prefix = '明天'
        }
        if (prefix == '') {
            prefix = (inputTime.getFullYear() == currentTime.getFullYear() ? '' : inputTime.getFullYear().toString() +
                '年') + (inputTime.getMonth() + 1).toString() + '月' + inputTime.getDate() + '日 '
        }
        prefix += getPeriod(inputTime.getHours()) + ' '
        prefix += inputTime.getHours().toString() + ':'
        if (inputTime.getMinutes() < 10) {
            prefix += '0'
        }
        prefix += inputTime.getMinutes().toString() + ' '
        prefix += getWeekName(inputTime.getDay())
        return prefix
    },
    wait(n) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                return resolve()
            }, n * 1000);
        })
    },
    
}

const check = {
    isArray(e) {
        return Array.isArray(e)
    },
    isInt(e) {
        return (typeof (e) == 'number') && (parseInt(e) == e)
    },
    isNumber(e) {
        return (typeof (e) == 'number')
    },
    isString(e) {
        return typeof (e) == 'string'
    },
    throwError(reason) {
        throw new Error(reason)
    }
}